package com.thon.controller.util;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;

/**
 * @Author: Chen
 * @Date: 2016/4/23 13:42
 * @Email: wirechen@gmail.com
 * @Description:
 */
public class MailTool {
    public static void main(String[] args) throws MessagingException, GeneralSecurityException {
        Properties props = new Properties();

        // 开启debug调试
        props.setProperty("mail.debug", "true");
        // 发送服务器需要身份验证
        props.setProperty("mail.smtp.auth", "true");
        // 设置邮件服务器主机名
        props.setProperty("mail.host", "smtp.163.com");
        // 发送邮件协议名称
        props.setProperty("mail.transport.protocol", "smtp");

        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.ssl.socketFactory", sf);

        Session session = Session.getInstance(props);

        Message msg = new MimeMessage(session);
        msg.setSubject("邮箱验证");
        StringBuilder builder = new StringBuilder();
        builder.append("恭喜收到這封郵件");
        builder.append("\n时间 " + new Date());
        msg.setText(builder.toString());
        try {
            msg.setFrom(new InternetAddress("15680800517@163.com","毕业论文管理系统"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        Transport transport = session.getTransport();
        transport.connect("smtp.163.com", "15680800517@163.com", "wei224529");
        //guaarilsvjgbbhac
        transport.sendMessage(msg, new Address[] { new InternetAddress("353478365@qq.com") });
        transport.close();
    }
}